package com.algorithm.backtrack;

/**
 * Created by mu on 2018/2/6.
 */
public class HorseBoard {
    private int move[][]= new int[][]{new int[]{2, 1}, new int[]{1, 2}, new int[]{-1, 2}, new int[]{-2, 1},
            new int[]{-2, -1}, new int[]{-1, -2}, new int[]{1, -2}, new int[]{2, -1}};
    private static int [][]chess=new int[12][12];
    private int cnt =0;
    private int count=0;
    public static void main(String[] args) {
        HorseBoard board = new HorseBoard();
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<12;j++)
            {
                if(i==0||i==1||i==11||i==10||j==0||j==1||j==11||j==10){
                    chess[i][j]=-1;
                }
            }
        }
        chess[2][2]=++board.cnt;
        board.horseBoard(2,2);
    }

    private void horseBoard(int x, int y) {
        for(int i=0;i<8;i++){
            int a=x+move[i][0];
            int b=y+move[i][1];
            if(chess[a][b]==0){
                chess[a][b]=++cnt;
                if(cnt <64){
                    horseBoard(a,b);
                }
                else
                {
                    System.out.format("第%d个棋盘\n",++count);
                    for(int g=2;g<10;g++){
                        for(int k=2;k<10;k++){
                            System.out.format(" %d ",chess[g][k]);
                        }
                        System.out.println();
                    }
                }
                chess[a][b]=0;
                --cnt;
            }
        }
    }
}
